<template>
  <div class="login">
    <img alt="Vue logo" src="../assets/logo.png">
    <div id="login">
      <el-card class="login-card">
        <div slot="header" class="clearfix">
          <h1>系统登录</h1>
        </div>
        <div class="text item">
          <el-form :model="loginForm" status-icon :rules="rules" ref="loginForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="登录账号" prop="userName">
              <el-input type="text" v-model="loginForm.userName" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="登录密码" prop="userPass">
              <el-input type="password" v-model="loginForm.userPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('loginForm')">马上登录</el-button>
              <el-button @click="resetForm('loginForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
  </div>
</template>

<style scoped>
#login {
  width: 400px;
  margin: 0 auto;
}
</style>

<script>
// @ is an alias to /src


export default {
  name: 'Login',
  components: {
  },
  data() {
      var validateName = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入登录账号'));
        } else {
          if (this.loginForm.userPass !== '') {
            this.$refs.loginForm.validateField('userPass');
          }
          callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入登录密码'));
        } else {
          callback();
        }
      };
      return {
        loginForm: {
          userName: '',
          userPass: ''
        },
        rules: {
          userName: [
            { validator: validateName, trigger: 'blur' }
          ],
          userPass: [
            { validator: validatePass, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            //alert('submit!');
            this.$router.push("/home");
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
}
</script>
